<?php

namespace App\Shop\Controllers;

use App\Admin\Extensions\Table\ServiceSelectTable;
use App\Admin\Extensions\Table\ShopSelectTable;
use App\Admin\Repositories\Card;
use App\Models\Area;
use App\Models\Service;
use App\Models\ServiceCategory;
use App\Models\ShopUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CardController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Card(), function (Grid $grid) {
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("shop_id",$shop_id);
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('price');
            $grid->column('days');
            $grid->column('discount','服务折扣');
            $grid->column('range')->using(\App\Models\Card::getRanges())->label();
            $grid->column('status')->radio(['正常','隐藏'],true)->label(['info','danger']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['title']);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Card(), function (Form $form) {
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $form->hidden('shop_id')->default($shop_id)->value($shop_id);
            $form->text('title')->required();
            $form->currency('price')->symbol("￥")->default(0.00)->required();
            $form->text('discount',"服务折扣")->prepend('折')->width(3)->defaultAttribute('placeholder', 0)->help("折扣0-10折, 10折表示不优惠,0折表示满折优惠")->default(0.00)->required();
            $form->number('days')->min(1)->default(1)->required();
            $form->radio('range')
                ->options(\App\Models\Card::getRanges())
                ->default(0)->when(0,function (Form $form){
                    $form->number('number')->min(1)->default(1)->required();
                })->when(1,function (Form $form) use ($shop_id){
                    $services = Service::query()->where("shop_id",$shop_id)->pluck("title","id");
                    $form->table('services','所属服务',function ($form) use ($services) {
                        $form->select("s_id","服务")->options($services);
                        $form->number("number","免费次数")->default(0);
                    });
                });
            $form->rate("rates.0","上级分销用户分成比例");
            $form->rate("rates.1","上级上级分销用户分成比例");
            $form->radio('status')->options(['正常','隐藏'])->default(0);
            $form->ckeditor("content","说明详情");
        });
    }
}
